package restlet.runtime;

import java.util.Date;

import org.restlet.Context;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.Variant;

import restlet.handlers.DeleteHandler;
import restlet.handlers.GetHandler;
import restlet.handlers.PostHandler;
import restlet.handlers.PutHandler;

/**
 * 
 * 
 */
public class CustomResource extends Resource {

	RESTResource resourceMediaTypeHandler;
	private Request request;
	private Response response;

	public CustomResource(Context context, Request request, Response response) {
		super(context, request, response);
		this.request = request;
		this.response = response;

		resourceMediaTypeHandler = (RESTResource) request
				.getAttributes().get(RESTResource.class);

		String path = request.getResourceRef().getPath();
		System.out.println(path);

		resourceMediaTypeHandler.configure(this);
	}

	/**
	 * GET - VIEW - READ - SHOW
	 */
	@Override
	public Representation represent(Variant variant) throws ResourceException {
		System.out.println("Received a GET/VIEW/READ/SHOW at " + new Date());

		GetHandler handler = resourceMediaTypeHandler.getGetHandlers().get(
				variant.getMediaType());
		return handler.getResource(request, response, variant);
	}

	/**
	 * PUT - EDIT - CHANGE - REPLACE
	 */
	@Override
	public void storeRepresentation(Representation entity)
			throws ResourceException {
		System.out.println("Received a PUT/EDIT/CHANGE/REPLACE at "
				+ new Date());

		PutHandler handler = resourceMediaTypeHandler.getPutHandlers().get(
				entity.getMediaType());
		handler.putResource(request, response, entity);

	}

	/**
	 * POST - NEW - ADD - CREATE
	 */
	@Override
	public void acceptRepresentation(Representation entity)
			throws ResourceException {
		System.out.println("Received a POST/NEW/ADD/CREATE at " + new Date());

		PostHandler handler = resourceMediaTypeHandler.getPostHandlers().get(
				entity.getMediaType());
		handler.postResource(request, response, entity);
	}

	/**
	 * DELETE - REMOVE
	 */
	public void removeRepresentations() throws ResourceException {
		System.out.println("Received a DELETE/REMOVE at " + new Date());

		DeleteHandler handler = resourceMediaTypeHandler.getDeleteHandler();
		handler.deleteResource(request, response);
	}

}
